<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProfissaoDAO
 *
 * @author instrutor
 */
class ProfissaoDAO {

    public function listar() {
        $pdo = new ConnectionFactory();
        $conexao = $pdo->getConnection();
        $query = $conexao->prepare('select * from PROFISSAO ');
        $query->execute();
        $dados = array();
        while ($linha = $query->fetch(PDO::FETCH_ASSOC)) {
            $dados[] = $linha;
        }
        return $dados;
    }

    public function incluir() {
        $pdo = new ConnectionFactory();
        $conexao = $pdo->getConnection();
        try {
            $conexao->beginTransaction();
            $query = $conexao->prepare('insert into PROFISSAO(DESCRICAO,ATIVO) values (?,?) ');
            $query->bindParam(1, $this->__get('descricao'), PDO::PARAM_STR, 45);
            $query->bindParam(2, $this->__get('ativo'), PDO::PARAM_STR, 1);
            $query->execute();
            $conexao->commit();
        } catch (Exception $e) {
            $conexao->rollback();
        }
    }

    public function buscar($id) {
        $pdo = new ConnectionFactory();
        $conexao = $pdo->getConnection();
        $query = $conexao->prepare('select * from PROFISSAO where ID_PROFISSAO = ?');
        $query->bindParam(1, $id, PDO::PARAM_INT, 10);
        $query->execute();

        while ($linha = $query->fetch(PDO::FETCH_ASSOC)) {
            foreach ($linha as $campo => $valor) {
                $this->__set($campo, $valor);
            }
        }
    }

    public function excluir() {
        $pdo = new ConnectionFactory();
        $conexao = $pdo->getConnection();
        try {
            $conexao->beginTransaction();
            $query = $conexao->prepare('delete from PROFISSAO where ID_PROFISSAO = ? ');
            $query->bindParam(1, $this->__get('id'), PDO::PARAM_INT, 10);
            $query->execute();
            $conexao->commit();
        } catch (Exception $e) {
            $conexao->rollback();
        }
    }

    
    public function atualizar(){
        
        $pdo = new ConnectionFactory();
        $conexao = $pdo->getConnection();
        try {
            $conexao->beginTransaction();
            $sql= 'update PROFISSAO set DESCRICAO = ?, ATIVO = ? where ID_PROFISSAO = ? ';
            $query = $conexao->prepare($sql);
            $query->bindParam(1, $this->__get('descricao'), PDO::PARAM_STR, 45);
            $query->bindParam(2, $this->__get('ativo'), PDO::PARAM_STR, 1);
            $query->bindParam(3, $this->__get('id'), PDO::PARAM_INT, 10);
            $query->execute();
            $conexao->commit();
        } catch (Exception $e) {
            $conexao->rollback();
        } 
        
        
        
    }
    
    
}

?>
